package com.boot.core.util

import com.boot.core.define.PoliceLevel
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
import javax.servlet.http.HttpServletRequest


object ManagerKit {


    const val POLICE_ID = "police_id"

    const val POLICE_NUMBER = "police_number"

    const val POLICE_LEVEL = "police_level"

    const val OUTSIDE = "outside"

    const val ORG_NUM = "org_num"

    private fun getHttpServletRequest(): HttpServletRequest {
        return (RequestContextHolder.getRequestAttributes() as ServletRequestAttributes).request
    }

    fun getPoliceNumber(): String {
        return if(getHttpServletRequest().getAttribute(POLICE_NUMBER)!=null)
            getHttpServletRequest().getAttribute(POLICE_NUMBER).toString()
        else
            getHttpServletRequest().getParameter(POLICE_NUMBER).toString()
    }

    fun isCaptain(): Boolean {
        if(getHttpServletRequest().getAttribute(POLICE_LEVEL)!=null)
            return getHttpServletRequest().getAttribute(POLICE_LEVEL) as? Int == PoliceLevel.CAPTAIN.value
        else
            return Integer.parseInt(getHttpServletRequest().getParameter(POLICE_LEVEL)) == PoliceLevel.CAPTAIN.value
    }

    fun isOfficer(): Boolean {
        if(getHttpServletRequest().getAttribute(POLICE_LEVEL)!=null)
            return getHttpServletRequest().getAttribute(POLICE_LEVEL) as? Int == PoliceLevel.OFFICER.value
        else
            return Integer.parseInt(getHttpServletRequest().getParameter(POLICE_LEVEL)) == PoliceLevel.OFFICER.value
    }

    fun isOutside(): Boolean {
        return getHttpServletRequest().getAttribute(OUTSIDE) as? Int == 1
    }

    fun getOrgNum(): String {
        return if(getHttpServletRequest().getAttribute(ORG_NUM)!=null)
            getHttpServletRequest().getAttribute(ORG_NUM).toString()
        else
            getHttpServletRequest().getParameter(ORG_NUM).toString()
    }

    fun getPoliceId(): Int {
//        return getHttpServletRequest().getAttribute(POLICE_ID) as Int
        return if(getHttpServletRequest().getAttribute(POLICE_ID)!=null)
            getHttpServletRequest().getAttribute(POLICE_ID) as Int
        else
            getHttpServletRequest().getParameter(POLICE_ID) as Int
    }

    fun onlyPolice(policeNumber: Int): Boolean {
        return !isCaptain() && getPoliceId() == policeNumber
    }
}